package ar.edu.sovietrussia.customcomponents.util;

import java.util.Map;

import org.apache.wicket.authroles.authorization.strategies.role.annotations.AuthorizeInstantiation;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;

import users.LibrarianRole;
import users.Person;
import ar.edu.sovietrussia.customcomponents.maincomponentviews.MainLibrarianComponent;
import ar.edu.sovietrussia.customcomponents.maincomponentviews.MainUserComponent;

/**
 * Component; knows where to go back, this is, the MainUserComponent or the MainLibrarianComponent
 */

@AuthorizeInstantiation({ "USER", "ADMIN" })
public abstract class BackToMainWindowGenericComponent extends Panel implements IFeedbacker, IDoReplaces {

    private static final long serialVersionUID = -5619432672207928003L;

    public BackToMainWindowGenericComponent(final String id) {
        super(id);
    }

    public BackToMainWindowGenericComponent(final String id, final IModel<?> aModel) {
        super(id, aModel);
    }

    public void goBack() {
        this.replaceWith(this.determineGoBackPanel(this.getId()));
    }

    public Panel determineGoBackPanel(final String id) {
        Person aModel = (Person) this.getSession().getAttribute("model");
        if (aModel.getUserAccount().getRole().equals(LibrarianRole.getInstance())) {
            return new MainLibrarianComponent(id) {

                private static final long serialVersionUID = 1L;

                @Override
                public FeedbackPanel getFeedbackTarget() {
                    return BackToMainWindowGenericComponent.this.getFeedbackTarget();
                }

                @Override
                public Map<String, WrapperPanel> getReplaceableComponents() {
                    return BackToMainWindowGenericComponent.this.getReplaceableComponents();
                }

            };
        } else {
            return new MainUserComponent(id);
        }
    }

}
